python之soap(soaplib(server),suds(client)) 您所在的位置:网站首页 wget wsdl python之soap(soaplib(server),suds(client))

python之soap(soaplib(server),suds(client))

2023-04-03 17:07| 来源: 网络整理| 查看: 265

如何用python实现soap协议并搭建webservice

 

网上搜索了一番,用python实现soap的库有好几个,soappy,soaplib,suds等。但是SOAPpy这个库已经好几年没人去维护更新了,所以不予考虑,而suds这个库用来构建soap服务端比较麻烦,所以最终锁定soaplib来实现服务端发布,而用suds实现客户端获取;

 

第一步:安装soaplib

 

http://pypi.python.org/pypi/soaplib/0.8.1

写道 安装挺简单的:下载后解压进入到目录soaplib-0.8.1, --> sudo python setup.py install即可 安装后在我本机的路径是在:/usr/local/lib/python2.6/dist-packages/下

 

    * 想要运行soaplib还必须依赖 libxml2及libxslt这二个包;google之后总结以下安装过程

 

第二步:安装libxml2

写道 1. wget ftp://xmlsoft.org/libxml2/libxml2-sources-2.7.8.tar.gz 2. tar -xvzf libxml2-sources-2.7.8.tar.gz 3. cd libxml2-2.7.8 4. ./configure --prefix=/usr/local/libxml2 5. make 6. sudo make install

    

    * 当一切都没有错的时候,你会在/usr/local里看到生成一个libxml2的文件夹

 

第三步:安装libxslt

写道 1. wget ftp://xmlsoft.org/libxslt/libxslt-1.1.26.tar.gz 2. tar -xvzf libxslt-1.1.26.tar.gz 3. cd libxslt-1.1.26 4. ./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2/ 5. make 6. sudo make install

    

    * 当一切都没有错的时候,你会在/usr/local里看到生成一个libxslt的文件夹

 

第四步:复制文件

写道 将libxml2及libxslt文件夹复制到soaplib文件夹下,即/usr/local/lib/python2.6/dist-packages/soaplib-0.8.1-py2.6.egg/soaplib

 

    至此,你可以使用soaplib开始来发布你的webservice了

 

    . 下面是一个简单的'hello world'的server.py及client.py

#server.py from soaplib.service import soapmethod from soaplib.serializers.primitive import String, Integer, Array from soaplib.wsgi_soap import SimpleWSGISoapApp class HelloWorldService(SimpleWSGISoapApp): @soapmethod(String, _returns=String) def says(self,name): return name if __name__=='__main__': try: from wsgiref.simple_server import make_server server = make_server('192.168.0.219', 7889, HelloWorldService()) print "listening on 0.0.0.0:7889" server.serve_forever() except ImportError: print "Error: example server code requires Python >= 2.5"

 

 

    . soaplib实现client

#client.py from server import HelloWorldService def make_client(): from soaplib.client import make_service_client client = make_service_client('http://192.168.0.219:7889', HelloWorldService()) return client a = make_client() print a.says('hello,world')

 

   

   . 下面看看suds如何实现client

  https://fedorahosted.org/releases/s/u/suds/python-suds-0.4.tar.gz

写道 安装也同soaplib很简单,解压进入目录,--> sudo python setup.py install即可 安装后在我本机的路径是在:/usr/local/lib/python2.6/dist-packages/下

 

#client.py from suds.client import Client url = "http://192.168.0.219:7889?wsdl" client = Client(url) print client.service.says('hello world')

 

 

  . 通过比较可以看出,用suds更简洁更方便,

  .https://fedorahosted.org/suds/wiki/Documentation

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有